<?php

namespace App\Controller;

class CompaniesController extends AppController
{

    public function initialize()
    {
        parent::initialize();
    }

    /*
     * create by phpstormgetRecommendInfo
     * author：Ming
     * date：2020.10.12
     * ps：获取企业展厅审核
     * */
    public function setVerifyStatus()
    {
        $conventionsId = $this->request->getData("conventionsId");
        $code = $this->request->getData("code");
        if (empty($conventionsId)) {
            $this->G->error("conventionsId_error");
            return null;
        }
        if (!is_numeric($code)) {
            $this->G->error("code_error");
            return null;
        }
        $this->loadModel('Conventions');
        $this->Conventions->query()
                          ->update()
                          ->set(['verify_status' => $code])
                          ->where(['id' => $conventionsId])
                          ->execute();
        $this->G->success("Success");
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.10.12
     * ps：获取企业展厅列表
     * */
    public function getCompanyConvention()
    {
        $page = empty($this->request->getData("page")) ? 1 : $this->request->getData("page");
        $companyId = $this->request->getData("companyId");
        $where = [];
        if (!empty($companyId)) {
            $where['company_id'] = $companyId;
        }
        $this->loadModel('Conventions');
        $conventions = $this->Conventions->find()
                                         ->where([$where])
                                         ->order(['id desc'])
                                         ->limit(30)
                                         ->page($page);
        $re = [];
        $count = $conventions->count();
        $re['pageTotal'] = ceil($count / 30);
        $conventions = $conventions->toArray();
        $re['list'] = [];
        if (!empty($conventions)) {
            foreach ($conventions as $k => $v) {
                $conventions[$k]['pic_album'] = empty($v['pic_album']) ? [] : json_decode($v['pic_album'], true);
                $re['list'][] = $conventions[$k];
            }
        }
        $this->G->success("Success", $re);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.10.12
     * ps：获取推荐会展列表
     * */
    public function getConventionList()
    {
        $page = empty($this->request->getData("page")) ? 1 : $this->request->getData("page");
        $title = $this->request->getData("title");
        $where = [];
        if (!empty($title)) {
            $where['title like'] = '%' . $title . '%';
        }
        $companies = $this->Companies->find()
                                     ->select(['id', 'title'])
                                     ->where([$where])
                                     ->toArray();
        $ids = [0];
        $company_infos = [];
        if (!empty($companies)) {
            $ids = array_column($companies, 'id');
            foreach ($companies as $k => $v) {
                $company_infos[$v['id']] = $v['title'];
            }
        }
        $this->loadModel('HomeConventions');
        $home_conventions = $this->HomeConventions->find()
                                                  ->where(['company_id in' => $ids])
                                                  ->order(['order_num desc'])
                                                  ->limit(30)
                                                  ->page($page);
        $re = [];
        $count = $home_conventions->count();
        $re['pageTotal'] = ceil($count / 30);
        $home_conventions = $home_conventions->toArray();
        $re['list'] = [];
        if (!empty($home_conventions)) {
            foreach ($home_conventions as $k => $v) {
                $home_conventions[$k]['start_date'] = date('Y-m-d H:i:s', $v['start_date']);
                $home_conventions[$k]['end_date'] = date('Y-m-d H:i:s', $v['end_date']);
                $home_conventions[$k]['title'] = empty($company_infos[$v['company_id']]) ? '' : $company_infos[$v['company_id']];
                $re['list'][] = $home_conventions[$k];
            }
        }
        $this->G->success("Success", $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：获取首页推荐会展信息
   * */
    public function getConventionInfo()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $this->loadModel('HomeConventions');
        $home_conventions = $this->HomeConventions->findById($id)->first();
        if (empty($home_conventions)) {
            $this->G->error("company_error");
            return null;
        }
        $home_conventions['start_date'] = empty($home_conventions['start_date']) ? '' : date('Y-m-d H:i:s', $home_conventions['start_date']);
        $home_conventions['end_date'] = empty($home_conventions['end_date']) ? '' : date('Y-m-d H:i:s', $home_conventions['end_date']);
        $this->G->success('Success', $home_conventions);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：删除首页推荐会展
   * */
    public function deleteConvention()
    {
        $id = $this->request->getData("id");
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $this->loadModel('HomeConventions');
        $this->HomeConventions->deleteAll(['id' => $id]);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：修改首页推荐会展
   * */
    public function updateConvention()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $companyId = $this->request->getData("companyId");
        $is_resident = $this->request->getData("isResident");
        $startDate = $this->request->getData("startDate");
        $endDate = $this->request->getData("endDate");
        $img = $this->request->getData("img");
        $orderNum = $this->request->getData("orderNum");
        $enabled = $this->request->getData("enabled");
        $this->loadModel('HomeConventions');
        $home_convention = $this->HomeConventions->newEntity();
        $home_convention->id = $id;
        $home_convention->company_id = $companyId;
        $home_convention->update_id = $this->Jwt->get('user_id');
        $home_convention->img = empty($img) ? '' : $this->G->setImgUrl($img);
        $home_convention->start_date = empty($startDate) ? 0 : strtotime($startDate);
        $home_convention->end_date = empty($endDate) ? 0 : strtotime($endDate);
        $home_convention->is_resident = empty($is_resident) ? 0 : $is_resident;
        //        $home_recommend_companies->is_slide = empty($isSlide) ? 0:$isSlide;
        $home_convention->enabled = $enabled == 1 ? 1 : 0;
        $home_convention->order_num = empty($orderNum) ? 0 : $orderNum;
        $this->HomeConventions->save($home_convention);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：新增首页推荐会展
   * */
    public function addConvention()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $companyId = $this->request->getData("companyId");
        $is_resident = $this->request->getData("isResident");
        $startDate = $this->request->getData("startDate");
        $endDate = $this->request->getData("endDate");
        $img = $this->request->getData("img");
        $orderNum = $this->request->getData("orderNum");
        $enabled = $this->request->getData("enabled");
        $this->loadModel('HomeConventions');
        $home_conventions = $this->HomeConventions->findByCompanyId($companyId)->select(['id'])->first();
        if (!empty($home_conventions)) {
            $this->G->error("company_error");
            return null;
        }
        $home_convention = $this->HomeConventions->newEntity();
        $home_convention->company_id = $companyId;
        $home_convention->create_id = $this->Jwt->get('user_id');
        $home_convention->img = empty($img) ? '' : $this->G->setImgUrl($img);
        $home_convention->start_date = empty($startDate) ? 0 : strtotime($startDate);
        $home_convention->end_date = empty($endDate) ? 0 : strtotime($endDate);
        $home_convention->is_resident = empty($is_resident) ? 0 : $is_resident;
        //        $home_recommend_companies->is_slide = empty($isSlide) ? 0:$isSlide;
        $home_convention->enabled = $enabled == 1 ? 1 : 0;
        $home_convention->order_num = empty($orderNum) ? 0 : $orderNum;
        $this->HomeConventions->save($home_convention);
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.10.12
     * ps：获取企业商品列表
     * */
    public function getProductList()
    {
        $page = empty($this->request->getData("page")) ? 1 : $this->request->getData("page");
        $title = $this->request->getData("title");
        $companyId = $this->request->getData("companyId");
        $verifyStatus = $this->request->getData("verifyStatus");
        $where = [];
        $where['is_delete'] = 0;
        if (!empty($title)) {
            $where['title like'] = '%' . $title . '%';
        }
        $this->loadModel('ProductCategories');
        if (!empty($companyId)) {
            $product_categories = $this->ProductCategories->find()
                                                          ->select(['id'])
                                                          ->where(['company_id' => $companyId])
                                                          ->toArray();
            $where['product_category_id in'] = empty($product_categories) ? [0] : array_column($product_categories, 'id');
        }
        if (isset($verifyStatus) && is_numeric($verifyStatus)) {
            $where['verify_status'] = $verifyStatus;
        }
        $this->loadModel('Products');
        $products = $this->Products->find()
                                   ->where([$where])
                                   ->order(['id desc'])
                                   ->limit(30)
                                   ->page($page);
        $re = [];
        $count = $products->count();
        $re['pageTotal'] = ceil($count / 30);
        $products = $products->toArray();
        $re['list'] = [];
        if (!empty($products)) {
            $product_categories = $this->ProductCategories->find()
                                                          ->select(['id', 'company_id'])
                                                          ->where(['id in' => array_column($products, 'product_category_id')])
                                                          ->toArray();
            $product_category = [];
            if (!empty($product_categories)) {
                foreach ($product_categories as $k => $v) {
                    $product_category[$v['id']] = $v['company_id'];
                }
                $companies = $this->Companies->find()
                                             ->select(['id', 'title'])
                                             ->where(['id in' => array_column($product_categories, 'company_id')])
                                             ->toArray();
                $company_infos = [];
                if (!empty($companies)) {
                    foreach ($companies as $k => $v) {
                        $company_infos[$v['id']] = $v['title'];
                    }
                }
            }
            foreach ($products as $k => $v) {
                $products[$k]['exp'] = empty($v['exp']) ? '' : date('Y-m-d H:i:s', $v['exp']);
                $products[$k]['companyId'] = $product_category[$v['product_category_id']];
                $products[$k]['companyTitle'] = $company_infos[$product_category[$v['product_category_id']]];
                $re['list'][] = $products[$k];
            }
        }
        $this->G->success("Success", $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：查看商品信息
   * */
    public function productInfo()
    {
        $id = $this->request->getData("id");
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $this->loadModel('Products');
        $products = $this->Products->findById($id)->first();
        $products['exp'] = empty($products['exp']) ? '' : date('Y-m-d H:i:s', $products['exp']);
        $this->loadComponent('TextareaCache');
        $content = $this->TextareaCache->getVal('products', $id);
        $this->loadComponent('ContentFormat');
        $products['content'] = empty($content) ? '' : $this->ContentFormat->add($content['content']);
        $this->G->success('Success', $products);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：企业商品审核
   * */
    public function setProductVerify()
    {
        $ids = $this->request->getData("ids");
        $check = $this->request->getData("check");
        if (!is_array($ids)) {
            $this->G->error("ids_error");
            return null;
        }
        if (!in_array($check, [1, 10])) {
            $this->G->error("check_error");
            return null;
        }
        $this->loadModel('Products');
        $this->Products->query()
                       ->update()
                       ->set(['verify_status' => $check])
                       ->where(['id in' => $ids])
                       ->execute();
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.10.12
     * ps：获取推荐企业列表
     * */
    public function getRecommendList()
    {
        $page = empty($this->request->getData("page")) ? 1 : $this->request->getData("page");
        $title = $this->request->getData("title");
        $where = [];
        if (!empty($title)) {
            $where['title like'] = '%' . $title . '%';
        }
        $companies = $this->Companies->find()
                                     ->select(['id', 'title'])
                                     ->where([$where])
                                     ->toArray();
        $ids = [0];
        $company_infos = [];
        if (!empty($companies)) {
            $ids = array_column($companies, 'id');
            foreach ($companies as $k => $v) {
                $company_infos[$v['id']] = $v['title'];
            }
        }
        $this->loadModel('HomeRecommendCompanies');
        $home_recommend_companies = $this->HomeRecommendCompanies->find()
                                                                 ->where(['company_id in' => $ids])
                                                                 ->order(['order_num desc'])
                                                                 ->limit(30)
                                                                 ->page($page);
        $re = [];
        $count = $home_recommend_companies->count();
        $re['pageTotal'] = ceil($count / 30);
        $home_recommend_companies = $home_recommend_companies->toArray();
        $re['list'] = [];
        if (!empty($home_recommend_companies)) {
            foreach ($home_recommend_companies as $k => $v) {
                $home_recommend_companies[$k]['start_date'] = empty($v['start_date']) ? '' : date('Y-m-d H:i:s', $v['start_date']);
                $home_recommend_companies[$k]['end_date'] = empty($v['end_date']) ? '' : date('Y-m-d H:i:s', $v['end_date']);
                $home_recommend_companies[$k]['title'] = empty($company_infos[$v['company_id']]) ? '' : $company_infos[$v['company_id']];
                $re['list'][] = $home_recommend_companies[$k];
            }
        }
        $this->G->success("Success", $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：获取首页推荐企业信息
   * */

    public function getRecommendInfo()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $this->loadModel('HomeRecommendCompanies');
        $home_recommend_companies = $this->HomeRecommendCompanies->findById($id)->first();
        if (empty($home_recommend_companies)) {
            $this->G->error("home_recommend_company_error");
            return null;
        }

        $this->loadModel('Companies');
        $companies = $this->Companies->findById($home_recommend_companies['company_id'])->first();

        $home_recommend_companies['companyTitle'] = $companies['title'];
        $home_recommend_companies['start_date'] = empty($home_recommend_companies['start_date']) ? '' : date('Y-m-d H:i:s', $home_recommend_companies['start_date']);
        $home_recommend_companies['end_date'] = empty($home_recommend_companies['end_date']) ? '' : date('Y-m-d H:i:s', $home_recommend_companies['end_date']);
        $this->G->success('Success', $home_recommend_companies);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：删除首页推荐企业
   * */
    public function deleteRecommend()
    {
        $id = $this->request->getData("id");
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $this->loadModel('HomeRecommendCompanies');
        $this->HomeRecommendCompanies->deleteAll(['id' => $id]);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：修改首页推荐企业
   * */
    public function updateRecommend()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $companyId = $this->request->getData("companyId");
        $is_resident = $this->request->getData("isResident");
        $startDate = $this->request->getData("startDate");
        $endDate = $this->request->getData("endDate");
        $img = $this->request->getData("img");
        $intro = $this->request->getData("intro");
        $orderNum = $this->request->getData("orderNum");
        $enabled = $this->request->getData("enabled");
        $this->loadModel('HomeRecommendCompanies');
        $home_recommend_companies = $this->HomeRecommendCompanies->newEntity();
        $home_recommend_companies->id = $id;
        $home_recommend_companies->company_id = $companyId;
        $home_recommend_companies->update_id = $this->Jwt->get('user_id');
        $home_recommend_companies->img = empty($img) ? '' : $this->G->setImgUrl($img);
        $home_recommend_companies->intro = empty($intro) ? 0 : $intro;
        $home_recommend_companies->start_date = empty($startDate) ? 0 : strtotime($startDate);
        $home_recommend_companies->end_date = empty($endDate) ? 0 : strtotime($endDate);
        $home_recommend_companies->is_resident = empty($is_resident) ? 0 : $is_resident;
        //        $home_recommend_companies->is_slide = empty($isSlide) ? 0:$isSlide;
        $home_recommend_companies->slide_img = empty($img) ? '' : $this->G->setImgUrl($img);
        $home_recommend_companies->enabled = $enabled == 1 ? 1 : 0;
        $home_recommend_companies->order_num = empty($orderNum) ? 0 : $orderNum;
        $this->HomeRecommendCompanies->save($home_recommend_companies);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：新增首页推荐企业
   * */
    public function addRecommend()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $companyId = $this->request->getData("companyId");
        $is_resident = $this->request->getData("isResident");
        $startDate = $this->request->getData("startDate");
        $endDate = $this->request->getData("endDate");
        $img = $this->request->getData("img");
        $intro = $this->request->getData("intro");
        $orderNum = $this->request->getData("orderNum");
        $enabled = $this->request->getData("enabled");
        $this->loadModel('HomeRecommendCompanies');
        $home_recommend_companies = $this->HomeRecommendCompanies->findByCompanyId($companyId)->select(['id'])->first();
        if (!empty($home_recommend_companies)) {
            $this->G->error("company_error");
            return null;
        }
        $home_recommend_companies = $this->HomeRecommendCompanies->newEntity();
        $home_recommend_companies->company_id = $companyId;
        $home_recommend_companies->create_id = $this->Jwt->get('user_id');
        $home_recommend_companies->img = empty($img) ? '' : $this->G->setImgUrl($img);
        $home_recommend_companies->intro = empty($intro) ? 0 : $intro;
        $home_recommend_companies->start_date = empty($startDate) ? 0 : strtotime($startDate);
        $home_recommend_companies->end_date = empty($endDate) ? 0 : strtotime($endDate);
        $home_recommend_companies->is_resident = empty($is_resident) ? 0 : $is_resident;
        //        $home_recommend_companies->is_slide = empty($isSlide) ? 0:$isSlide;
        $home_recommend_companies->slide_img = empty($img) ? '' : $this->G->setImgUrl($img);
        $home_recommend_companies->enabled = $enabled == 1 ? 1 : 0;
        $home_recommend_companies->order_num = empty($orderNum) ? 0 : $orderNum;
        $this->HomeRecommendCompanies->save($home_recommend_companies);
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.10.12
     * ps：搜索企业
     * */
    public function searchCompany()
    {
        $name = $this->request->getData("name");
        $where = [];
        if (!empty($name)) {
            $where['title like'] = '%' . $name . '%';
        }
        $where['is_delete'] = 0;
        $where['state'] = 10;
        $companies = $this->Companies->find()
                                     ->select(['id', 'title'])
                                     ->where([$where])
                                     ->toArray();
        $this->G->success("Success", $companies);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：设置企业信息
   * */
    public function getList()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $page = $this->request->getData('page');
        $title = $this->request->getData('title');
        $where = [];
        if (!empty($title)) {
            $where['title like'] = '%' . $title . '%';
        }
        $this->loadModel('BizDicts');
        $this->loadModel('Zones');
        $this->loadModel('Countries');
        $companies = $this->Companies->find()
                                     ->where($where)
                                     ->order(['id desc'])
                                     ->limit(10)
                                     ->page($page);
        $re = [];
        $count = $companies->count();
        $re['pageTotal'] = ceil($count / 10);
        $companies = $companies->toArray();
        $re['list'] = [];
        if (!empty($companies)) {
            foreach ($companies as $k => $v) {
                $org_type = $this->BizDicts->findByValue($v['org_type'])->where(['type' => 'org_type'])->first();
                $staff_size = $this->BizDicts->findByValue($v['staff_size'])->where(['type' => 'staff_size'])->first();
                $zones = $this->Zones->findById($v['zone_id'])->first();
                $countries = $this->Countries->findById($v['country_id'])->first();
                $companies[$k]['org_type'] = empty($org_type) ? '' : $org_type['label'];
                $companies[$k]['staff_size'] = empty($staff_size) ? '' : $staff_size['label'];
                $companies[$k]['zone_id'] = empty($zones) ? '' : $zones['name'];
                $companies[$k]['country_id'] = empty($countries) ? '' : $countries['name'];
                $re['list'][] = $companies[$k];
            }
        }
        $this->G->success('Success', $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：设置企业会员组
   * */
    public function setLevel()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData('ids');
        $levelAroId = $this->request->getData('levelAroId');
        if (!is_array($ids)) {
            $this->G->error("ids_error");
            return null;
        }
        if (!is_numeric($levelAroId)) {
            $this->G->error("levelAroId_error");
            return null;
        }
        $this->CompanyLevels->query()
                            ->update()
                            ->set(['level_aro_id' => $levelAroId])
                            ->where(['company_id in' => $ids])
                            ->execute();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：设置企业状态
   * */
    public function setEnabled()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData('ids');
        $enabled = $this->request->getData('enabled');
        if (!is_array($ids)) {
            $this->G->error("ids_error");
            return null;
        }
        if (!in_array($enabled, [0, 1, 2])) {
            $this->G->error("enabled_error");
            return null;
        }
        $this->Companies->query()
                        ->update()
                        ->set(['enabled' => $enabled])
                        ->where(['id in' => $ids])
                        ->execute();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：企业审核
   * */
    public function setCheck()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData('ids');
        $check = $this->request->getData('check');
        if (!is_array($ids)) {
            $this->G->error("ids_error");
            return null;
        }
        if (!in_array($check, ['success', 'fail'])) {
            $this->G->error("check_error");
            return null;
        }
        $check = $check === 'success' ? 10 : 9;
        $this->Companies->query()
                        ->update()
                        ->set(['state' => $check])
                        ->where(['id in' => $ids])
                        ->execute();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.09.03
   * ps：获取企业信息
   * */
    public function getInfo()
    {
        if (!$this->request->is("post")) {
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData('id');
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $info = [];
        $this->loadModel('Companies');
        $this->loadModel('BizDicts');
        $this->loadModel('Zones');
        $this->loadModel('Countries');
        $this->loadModel('CompanyBankAccounts');
        $this->loadModel('CompanyInfos');
        $companies = $this->Companies->findById($id)->first();
        if (empty($companies)) {
            $this->G->error("company_error");
            return null;
        }
        $org_type = $this->BizDicts->findByValue($companies['org_type'])->where(['type' => 'org_type'])->first();
        //        if(empty($org_type)){
        //            $this->G->error("org_type_error");
        //            return null;
        //        }
        $staff_size = $this->BizDicts->findByValue($companies['staff_size'])->where(['type' => 'staff_size'])->first();
        //        if(empty($staff_size)){
        //            $this->G->error("staff_size_error");
        //            return null;
        //        }
        $zones = $this->Zones->findById($companies['zone_id'])->first();
        //        if(empty($zones)){
        //            $this->G->error("zone_error");
        //            return null;
        //        }
        $countries = $this->Countries->findById($companies['country_id'])->first();
        //        if(empty($countries)){
        //            $this->G->error("country_error");
        //            return null;
        //        }
        $company_bank_accounts = $this->CompanyBankAccounts->findByCompanyId($id)->first();
        //        if(empty($company_bank_accounts)){
        //            $this->G->error("company_bank_account_error");
        //            return null;
        //        }
        $company_infos = $this->CompanyInfos->findByCompanyId($id)->order(['id desc'])->first();
        //        if(empty($company_bank_accounts)){
        //            $this->G->error("company_bank_account_error");
        //            return null;
        //        }
        $info['info']['name'] = empty($company_infos) ? '' : $company_infos['name'];
        $info['info']['no'] = empty($company_infos) ? '' : $company_infos['no'];
        $info['info']['belong_org'] = empty($company_infos) ? '' : $company_infos['belong_org'];
        $info['info']['start_date'] = empty($company_infos) ? '' : $company_infos['start_date'];
        $info['info']['end_date'] = empty($company_infos) ? '' : $company_infos['end_date'];
        $info['info']['status'] = empty($company_infos) ? '' : $company_infos['status'];
        $info['info']['credit_code'] = empty($company_infos) ? '' : $company_infos['credit_code'];
        $info['info']['regist_capi'] = empty($company_infos) ? '' : $company_infos['regist_capi'];
        $info['info']['econ_kind'] = empty($company_infos) ? '' : $company_infos['econ_kind'];
        $info['info']['address'] = empty($company_infos) ? '' : $company_infos['address'];
        $info['info']['term_start'] = empty($company_infos) ? '' : $company_infos['term_start'];
        $info['info']['term_end'] = empty($company_infos) ? '' : $company_infos['term_end'];
        $info['info']['check_date'] = empty($company_infos) ? '' : $company_infos['check_date'];
        $info['info']['legalId'] = empty($company_infos) ? '' : $company_infos['legalId'];
        $info['info']['legalName'] = empty($company_infos) ? '' : $company_infos['legalName'];
        $info['id'] = $companies['id'];
        $info['title'] = $companies['title'];
        $info['logo'] = $companies['logo'];
        $info['license'] = $companies['license'];
        $info['legalPerson'] = $companies['legal_person'];
        $info['creditCode'] = $companies['credit_code'];
        $info['orgType'] = $companies['org_type'];
        $info['orgTypeTitle'] = empty($org_type['label']) ? '' : $org_type['label'];
        $info['zoneId'] = $companies['zone_id'];
        $info['zoneTitle'] = empty($zones['name']) ? '' : $zones['name'];
        $info['countryId'] = $companies['country_id'];
        $info['countryTitle'] = empty($countries['name']) ? '' : $countries['name'];
        $info['address'] = $companies['address'];
        $info['staffSize'] = $companies['staff_size'];
        $info['staffSizeTitle'] = empty($staff_size['label']) ? '' : $staff_size['label'];
        $info['state'] = $companies['state'];
        $info['feedback'] = $companies['feedback'];
        $info['account'] = empty($company_bank_accounts['account']) ? '' : $company_bank_accounts['account'];
        $info['bankCode'] = empty($company_bank_accounts['bank_code']) ? '' : $company_bank_accounts['bank_code'];
        $info['bankName'] = empty($company_bank_accounts['bank_name']) ? '' : $company_bank_accounts['bank_name'];
        $info['branchBankCode'] = empty($company_bank_accounts['branch_bank_code']) ? '' : $company_bank_accounts['branch_bank_code'];
        $info['branchBankName'] = empty($company_bank_accounts['branch_bank_name']) ? '' : $company_bank_accounts['branch_bank_name'];
        $info['provinceCode'] = empty($company_bank_accounts['province_code']) ? '' : $company_bank_accounts['province_code'];
        $info['provinceName'] = empty($company_bank_accounts['province_name']) ? '' : $company_bank_accounts['province_name'];
        $info['cityCode'] = empty($company_bank_accounts['city_code']) ? '' : $company_bank_accounts['city_code'];
        $info['cityName'] = empty($company_bank_accounts['city_name']) ? '' : $company_bank_accounts['city_name'];
        $info['orderNumber'] = empty($company_bank_accounts['order_number']) ? '' : $company_bank_accounts['order_number'];
        $info['acceptState'] = empty($company_bank_accounts['accept_state']) ? 0 : $company_bank_accounts['accept_state'];
        $this->G->success('Success', $info);
    }

    /**
     * 获取hmac_sha1签名的值
     *
     * @link 代码来自： http://www.educity.cn/develop/406138.html
     *
     * @param $str 源串
     * @param $key 密钥
     *
     * @return 签名值
     */
    function hmac_sha1($str, $key)
    {
        return hash_hmac("sha1", $str, $key, true);
    }


    function base64UrlEncode($str)
    {
        return str_replace('=', '', strtr(base64_encode($str), '+/', '-_'));
    }

}

?>
